package com.daxidi.cn.controller;


import com.daxidi.cn.entity.Nonce;
import com.daxidi.cn.service.INonceService;
import com.daxidi.cn.utils.ReturnResult;
import com.daxidi.cn.utils.ReturnResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 保存历史使用的nonce 每个nonce只能使用一次 前端控制器
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Log4j
@Api(tags = "折扣率模块接口")
@RestController
@RequestMapping("/api/v1/union/nonce")
public class NonceController {
    @Autowired
    private INonceService nonceService;

    @ApiOperation(value = "折扣率模块  添加接口")
    @ApiResponses({
            @ApiResponse(code = 148, message = "添加成功"),
            @ApiResponse(code = 149, message = "添加失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PostMapping("/")
    public ReturnResult addNonce(@RequestBody Nonce nonce){
        try {
            nonceService.addNonce(nonce);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("添加失败" + e);
            return ReturnResultUtils.returnFailMsg(149, "添加失败");
        }
    }

    @ApiOperation(value = "折扣率模块  删除接口")
    @ApiResponses({
            @ApiResponse(code = 150, message = "删除成功"),
            @ApiResponse(code = 151, message = "删除失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @DeleteMapping("/{id}")
    public ReturnResult deleteNonceById(@PathVariable("id") Integer shopId){
        try {
            nonceService.deleteNonceById(shopId);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("删除失败" + e);
            return ReturnResultUtils.returnFailMsg(151, "删除失败");
        }
    }

    @ApiOperation(value = "折扣率模块  修改接口")
    @ApiResponses({
            @ApiResponse(code = 152, message = "修改成功"),
            @ApiResponse(code = 153, message = "修改失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PutMapping("/{id}")
    public ReturnResult updateNonceById(@RequestBody Nonce nonce){
        try {
            nonceService.updateNonceById(nonce);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("修改失败" + e);
            return ReturnResultUtils.returnFailMsg(153, "修改失败");
        }
    }

    @ApiOperation(value = "折扣率模块  查询接口",notes = "count:  为 数据的总条数")
    @ApiResponses({
            @ApiResponse(code = 154, message = "查询成功"),
            @ApiResponse(code = 155, message = "查询失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping("/")
    public ReturnResult selectNoncesPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5") Integer limit){
        try {
            Map map = nonceService.selectNoncesPage(page, limit);
            return ReturnResultUtils.returnSuccess(map);
        } catch (Exception e) {
            log.error("查询失败" + e);
            return ReturnResultUtils.returnFailMsg(155, "查询失败");
        }
    }

}
